<?php
/**
 *  获取玩家邮件
 */

class GetEmailByUser extends APIBase {
	public $tag = "GetEmailByUser";
	public $isLogin = true;
	
	public function before() {
		$this->initMysql();
		$this->inithttp();
		$this->initCacheRedis();
		return true;
	}
			
	public function logic() {
	    $uid = $this->uid;
		$keys = $this->cache_redis->keys('hemailbyuser:'.$uid.':*');
		if(count($keys)>0){
			$data = array();
			for($i=1;$i<=count($keys);$i++){
				$row = $this->cache_redis->hGetAll('hemailbyuser:'.$uid.':'.$i);
				//过滤已删除的邮件
				if($row['is_read']!=9){
					$data[] = array('id'=>$i,'uid'=>$row['uid'],'mid'=>$row['mid'],
				                'is_read'=>$row['is_read'],'is_receive'=>$row['is_receive'],
							    'send_date'=>$row['send_date'],'update_date'=>$row['update_date'],
								'sender'=>$row['sender']
								);
				}
			}
			
			//按发送时间排序
		    for($k=1;$k<count($data);$k++){
			    for($i=0;$i<count($data)-$k;$i++){
				    $temp = '';
				    if($data[$i]['send_date'] < $data[$i+1]['send_date']){
					    $temp = $data[$i];
					    $data[$i] = $data[$i+1];
					    $data[$i+1] = $temp;
				    }
			    }
			}
		
			//按状态分类
			$unread = array();
			$unreceive = array();
			$read = array();
			$sort = array();
			for($i=0;$i<count($data);$i++){
				if($data[$i]['is_read']==0){
					$unread[] = $data[$i];
				}
				if($data[$i]['is_read']==1 && $data[$i]['is_receive']==0){
					$unreceive[] = $data[$i];
				}
				if($data[$i]['is_read']==1 && $data[$i]['is_receive']==1){
					$read[] = $data[$i];
				}
			}
			
			if(!empty($unreceive)){
				for($i=0;$i<count($unreceive);$i++){
				    array_push($unread,$unreceive[$i]);
			    }
			}
			if(!empty($read)){
				for($i=0;$i<count($read);$i++){
				    array_push($unread,$read[$i]);
			    }
			}
		
			foreach($unread as $key=>$val){
			    $hmg = $this->cache_redis->hMGet('hmg:'.$val['mid'],array('title','rid'));
				//判断邮件是否有附件
			    if($hmg['rid']==''){
					$sort[] = array('id'=>$val['id'],'title'=>$hmg['title'],'send_date'=>date('Y-m-d h:i:s',$val['send_date']),'is_read'=>$val['is_read']);
				}else{
					$sort[] = array('id'=>$val['id'],'title'=>$hmg['title'],'send_date'=>date('Y-m-d h:i:s',$val['send_date']),'is_read'=>$val['is_read'],'is_receive'=>$val['is_receive']);
				}
				
			}
			if(empty($sort)){
				$this->returnError(301,'用户暂无邮件！');
			}else{
				if(count($sort)<=30){
					$this->returnData($sort);
				}else{
					$count = count($sort);//玩家邮件总数
					$arr = array_slice($sort,0,29);//向客户端展现30封邮件
					//把多余邮件状态设置成9
					$num = (int)$count-30;
					$moreEmail = array_slice($sort,-$num);
                    foreach($moreEmail as $val){
						$this->cache_redis->hMset('hemailbyuser:'.$uid.':'.$val['id'],array('is_read'=>9));
					}
					$this->returnData($arr);
				}
			}
		}else{
			$this->returnError(301,'用户暂无邮件！');
		}
	}

	public function after() {
		$this->deinitDataRedis();
		$this->deinitMysql();
	}
}

?>
